library(dplyr)
library(readstata13)

## set to Dataversere replication file
survey <- read.dta13("Harris_Data/Harris 1976 ABC Presidential Election Survey, study no. 2624S-L/harris_s2624_voters_spss.dta")

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- as.character(2624)

# study year (year)
survey$year <- 1976

# geographic data (urban)
summary(survey$S13)
survey$urban <- as.character(survey$S13)

# geographic data (region)
survey$region <- as.character(survey$S11)
table(survey$region)

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
survey$inequality <- as.character(survey$Q6_2)
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
## union response "Not Sure" included separately
survey$union.self <- as.character(survey$F3_1)
survey$union.self[survey$F3_4 == "Yes"] <- "Not Sure"

survey$union.other <- as.character(survey$F3_2)
survey$union.other[survey$F3_4 == "Yes"] <- "Not Sure"

survey[survey$F3_4 == "Yes", c("union.self", "union.other", "F3_3")]

# employment (employed)
summary(survey$F1A)
survey$employed <- as.character(survey$F1A)

# employ self
survey$employed.self <- NA

# occupation
survey$occupation <- as.character(survey$F1B)
table(survey$occupation)

# ocup self
survey$occupation.self <- NA 

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
summary(survey$F2)
survey$educ <- as.character(survey$F2)                          

# household income (income)
survey$income <- as.character(survey$F4)
table(survey$income)

# age
survey$age <- as.character(survey$S4)
table(survey$age)

# race
survey$race <- as.character(survey$F7)
table(survey$race)

# politics (party)
survey$party <- as.character(survey$S3)
table(survey$party)

# politics (ideology)
survey$ideology <- as.character(survey$Q1D)
table(survey$ideology)

# gender
survey$gender <- as.character(survey$F8)
table(survey$gender)

# religion
survey$religion <- NA # could not find variable listed in spreadsheet, F5A/B


## factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- as.character(survey$Q6_1)
survey$dontcount <- as.character(survey$Q6_3)
survey$leftout <- as.character(survey$Q6_4)

## question place
survey$question_place <- "after party"

# subset
survey_2624SLvoters <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                                 "inequality", "inequality.variable", "union.self", "union.other",
                                 "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                                 "age", "race", "party", "ideology", "gender", "religion",
                                 "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                                 "question_place")]

#saveRDS(survey_2624SLvoters, file = "Harris_Data/survey_2624SLvoters.rds")


## there is a second dataset in the folder
## when you download it "harris_s2624_nonvoter_spss.dta"
## we don't include that dataset because they didn't
## include inequality question